import store from '@/store'

class common {
    checkPermissions(data) {
        const permissionsList = store.state.permissionsList
        const organizationId = store.state.employeeInfo.organization.id
        let obj = {}
        // 如果是管理员就直接返回true
        if (permissionsList.findIndex(item => item.id === `organization:admin:${organizationId}`) !== -1) {
            for (let i in data) {
                obj[i] = true
            }
        } else {
            for (let i in data) {
                const aType = typeof data[i]
                // 判断传过来的是数组还是字符串
                if (aType === 'string') {
                    let noSpacesStr = data[i].replace(/\s/g, '');
                    // 如果只是字符串，那么就判断是否有这个许可
                    const index = permissionsList.findIndex(aItem => aItem.id === noSpacesStr)
                    obj[i] = index !== -1;
                } else {
                    // 如果不是字符串， 那么就遍历传过来的数组，判断是否拥有其中一个
                    const arr = []
                    data[i].forEach(item => {
                        const index = permissionsList.findIndex(aItem => aItem.id === item)
                        arr.push(index !== -1)
                    })
                    obj[i] = arr.includes(true)
                }
            }
        }
        return obj
    }
    newDate(time, format = 'yyyy-mm-hh') {
        let date = new Date(time);
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        month = month < 10 ? '0' + month : month
        let day = date.getDate()
        day = day < 10 ? '0' + day : day
        let timeDate = ''
        if (format === 'yyyy-mm-hh') {
            timeDate = year + '-' + month + '-' + day
        } else if (format === 'yyyy-mm') {
            timeDate = year + '-' + month
        }
        return timeDate
    }
    // 获取年月
    newYearMonth(time) {
        let date = new Date(time);
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        month = month < 10 ? '0' + month : month
        return year + month;
    }
    getLastMonth(time) {
        const arr = time.split('-')
        let year = Number(arr[0])
        let month = Number(arr[1])
        if (month === 1) {
            year -= 1
            month = 12
        } else {
            month -= 1
        }
        return month < 10 ? `${year}-0${month}` : `${year}-${month}`
    }

    getDateRangeList(startDate, stopDate) {
        let newArr = []
        const startArr = startDate.split('-')
        const stopArr = stopDate.split('-')
        let startYear = Number(startArr[0])
        let stopYear = Number(stopArr[0])
        let startMonth = Number(startArr[1])
        let stopMonth = Number(stopArr[1])
        newArr.push(startDate)
        if (startYear < stopYear) {
            const num = 13 - startMonth + stopMonth - 1
            for (let a = 0; a < num; a++) {
                if (startMonth === 12) {
                    startMonth = 1
                } else if (startMonth < 12) {
                    startMonth++
                } else if (startMonth > 12) {
                    startMonth++
                }
                newArr.push(startMonth < 10 ? `${startYear}-0${startMonth}` : `${startYear}-${startMonth}`)
            }
        } else if (startYear === stopYear) {
            const num = stopMonth - startMonth
            for (let a = 0; a < num; a++) {
                startMonth++
                newArr.push(startMonth < 10 ? `${startYear}-0${startMonth}` : `${startYear}-${startMonth}`)
            }
        }
        return newArr
    }
    getTheFirstSixMonths(time, isReverse = false) {
        const arr = time.split('-')
        let newArr = []
        let year = Number(arr[0])
        let month = Number(arr[1])
        for (let a = 0; a < 6; a++) {
            if (month === 1) {
                year -= 1
                month = 12
            } else {
                month -= 1
            }
            newArr.push(month < 10 ? `${year}-0${month}` : `${year}-${month}`)
        }
        if (isReverse) {
            newArr.reverse()
        }
        return newArr
    }
    newDetailDate(time) {
        let date = new Date(time);
        let hour = date.getHours();
        hour = hour < 10 ? '0' + hour : hour;
        let minute = date.getMinutes();
        minute = minute < 10 ? '0' + minute : minute;
        let second = date.getSeconds();
        second = second < 10 ? '0' + second : second;

        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        month = month < 10 ? '0' + month : month;
        let day = date.getDate()
        day = day < 10 ? '0' + day : day
        return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
    }
    newYearDate(time) {
        let date = new Date(time);
        let year = date.getFullYear();
        let timeDate = ''
        timeDate = year + '年'

        return timeDate
    }
    //通过身份证号计算年龄、性别、出生日期
    getIdCardInfomation(userCard, num) {
        //获取出生日期
        let birth;
        if (num == 1) {
            birth = userCard.substring(6, 10) + "-" + userCard.substring(10, 12) + "-" + userCard.substring(12, 14);
            return birth;
        }
        //获取性别
        if (num == 2) {
            if (parseInt(userCard.substr(16, 1)) % 2 == 1) {
                return "男";
            } else {
                return "女";
            }
        }
        //获取年龄
        if (num === 3) {
            const myDate = new Date();
            const month = myDate.getMonth() + 1;
            const day = myDate.getDate();
            let age = myDate.getFullYear() - userCard.substring(6, 10) - 1;
            if (userCard.substring(10, 12) < month || userCard.substring(10, 12) === month && userCard.substring(12, 14) <= day) {
                age++;
            }
            return age;
        }
    }

    //通过身份证、以及某一时间年月日获取年龄、性别、出生日期
    getIdCardInfomationByTime(userCard, oringinTime, num) {
        function newDate(time, format = 'yyyy-mm-hh') {
            let date = new Date(time);
            let year = date.getFullYear();
            let month = date.getMonth() + 1;
            month = month < 10 ? '0' + month : month
            let day = date.getDate()
            day = day < 10 ? '0' + day : day
            let timeDate = ''
            if (format === 'yyyy-mm-hh') {
                timeDate = year + '-' + month + '-' + day
            } else if (format === 'yyyy-mm') {
                timeDate = year + '-' + month
            }
            return timeDate
        }
        let time = newDate(oringinTime);
        //获取出生日期
        let birth;
        if (num === 1) {
            birth = userCard.substring(6, 10) + "-" + userCard.substring(10, 12) + "-" + userCard.substring(12, 14);
            return birth;
        }
        //获取性别
        if (num === 2) {
            if (parseInt(userCard.substr(16, 1)) % 2 === 1) {
                return "男";
            } else {
                return "女";
            }
        }
        //获取年龄
        if (num === 3) {
            const myDate = new Date(time);
            const month = myDate.getMonth() + 1;
            const day = myDate.getDate();
            let age = myDate.getFullYear() - userCard.substring(6, 10) - 1;
            if (userCard.substring(10, 12) < month || userCard.substring(10, 12) === month && userCard.substring(12, 14) <= day) {
                age++;
            }
            return age;
        }
    }

    /**
 * @desc 格式化日期字符串
 * @param { Nubmer} - 时间戳
 * @returns { String } 格式化后的日期字符串
 */
    getFormatDate(timestamp) {

        // 补全为13位
        let arrTimestamp = (timestamp + '').split('');
        for (let start = 0; start < 13; start++) {
            if (!arrTimestamp[start]) {
                arrTimestamp[start] = '0';
            }
        }
        timestamp = arrTimestamp.join('') * 1;

        let minute = 1000 * 60;
        let hour = minute * 60;
        let day = hour * 24;
        let now = new Date().getTime();
        let diffValue = now - timestamp;

        // 如果本地时间反而小于变量时间
        if (diffValue < 0) {
            return '不久前';
        }
        // 计算差异时间的量级
        let dayC = diffValue / day;
        let hourC = diffValue / hour;
        let minC = diffValue / minute;

        // 数值补0方法
        let zero = function (value) {
            if (value < 10) {
                return '0' + value;
            }
            return value;
        };

        // if (monthC > 4) {
        //     // 超过1年，直接显示年月日
        //     return (function () {
        //         let date = new Date(timestamp);

        //         return date.getFullYear() + '年' + zero(date.getMonth() + 1) + '月' + zero(date.getDate()) + '日';
        //     })();
        // } else if (monthC >= 1) {
        //     return parseInt(monthC) + '月前';
        // } else if (weekC >= 1) {
        //     return parseInt(weekC) + '周前';
        // }else
        if (dayC > 3) {
            return (function () {
                let date = new Date(timestamp);

                return date.getFullYear() + '年' + zero(date.getMonth() + 1) + '月' + zero(date.getDate()) + '日';
            })();


        } else if (dayC >= 1 && dayC <= 3) {
            return parseInt(dayC) + '天前';
        } else if (hourC >= 1) {
            return parseInt(hourC) + '小时前';
        } else if (minC >= 1) {
            return parseInt(minC) + '分钟前';
        }

        return '刚刚';

    }

    isEmpty(val) {
        return [
            undefined,
            null,
            NaN,
            "",
            "null",
            "undefined",
            "NaN",
            "NULL",
            "UNDEFINED",
        ].includes(val);
    }
    // 是否是其他类的历史数据
    isOtherHistoryData(talentApplyEntryType, talentApplySubType) {
        return talentApplyEntryType == 'OTHERS' && talentApplySubType == 'NONE';
    }


    isNotEmptyObject(obj) {
        if (typeof obj === 'object') {
            for (let key in obj) {
                if (obj[key] !== undefined && obj[key] !== null && obj[key] !== '') {
                    return true;
                }
            }
        } else {
            return false;

        }
    }

    textTohtml(val) {
        return val.replace(/<(.+?)>/gi, "&lt;$1&gt;")
            .replace(/ /gi, "&nbsp;")
            .replace(/\n/gi, "<br/>");
    }
    htmlToText(val) {
        return val.replace(/<br\/>/g, "\n")
            .replace(/&nbsp;/g, " ")
            .replace(/&lt;$1&gt;/gi, "<(.+?)>");
    }


}

export default new common()
